/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.umlutils;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


/**
 * This class encapsulates an <code>org.eclipse.uml2.uml.State</code><BR>
 * 
 * @author Remi SCHNEKENBURGER
 */
public class Region extends NamedElement {
    
    /**
     * 
     */
    private org.eclipse.uml2.uml.Region uml2Region;
    
    /**
     * 
     * 
     * @param region 
     */
    public Region(org.eclipse.uml2.uml.Region region) {
        super(region);
        this.uml2Region = region;
    }
    
    /**
     * Get the encapsulated <code>State</code>.
     * 
     * @return Returns the uml2State.
     */
    public org.eclipse.uml2.uml.Region getUml2Region() {
        return uml2Region;
    }
    
    /**
     * Retrieve the initial state.
     * 
     * @return 
     */
    public com.cea.papyrus.umlutils.State getInitialState() {
    	
    	com.cea.papyrus.umlutils.State initState = null;
		org.eclipse.uml2.uml.Pseudostate currentPS = null;
    	boolean found = false;
    	
    	
    	Iterator vertexIt = getUml2Region().getSubvertices().iterator();
    	
    	// Try to find the initial pseudostate
    	while (vertexIt.hasNext() && !found) {
    		org.eclipse.uml2.uml.Vertex currentVertex = (org.eclipse.uml2.uml.Vertex) vertexIt.next();
    		
    		if (currentVertex instanceof org.eclipse.uml2.uml.Pseudostate) {
    			currentPS = (org.eclipse.uml2.uml.Pseudostate) currentVertex;
    			
    			if (currentPS.getKind() == org.eclipse.uml2.uml.PseudostateKind.INITIAL_LITERAL) {
    				found = true;
    			}
    		}
    	}

    	// get targetted state -- should have one outgoing transition only
    	org.eclipse.uml2.uml.Transition tr = (org.eclipse.uml2.uml.Transition) currentPS.getOutgoings().get(0);
    	com.cea.papyrus.umlutils.State targetState = new com.cea.papyrus.umlutils.State((org.eclipse.uml2.uml.State) tr.getTarget());
    	
    	if (targetState.getUml2State().isComposite()) {
    		com.cea.papyrus.umlutils.Region cRegion = new com.cea.papyrus.umlutils.Region((org.eclipse.uml2.uml.Region)targetState.getUml2State().getRegions().get(0));
        	initState = cRegion.getInitialState();
    	} else {
    		initState = targetState;
    	}
    	
    	
    	return initState;
    }

    /**
     * 
     * 
     * @return Simple states of current stateMachine
     */
    public List<State> getSimpleStates() {
        List<State> states = new LinkedList<State>() ;

        Iterator iElt = getUml2Region().getOwnedElements().iterator();
        while(iElt.hasNext()) {
        	org.eclipse.uml2.uml.Element currentElt = (org.eclipse.uml2.uml.Element) iElt.next();

        	if (currentElt instanceof org.eclipse.uml2.uml.State) {
        		com.cea.papyrus.umlutils.State currentState = new com.cea.papyrus.umlutils.State((org.eclipse.uml2.uml.State) currentElt) ;
        		
        		if (currentState.getUml2State().isSimple()) {
        			states.add(currentState);
        		} else {
        			// Check owned regions for other simple states
        			Iterator regionsIt = currentState.getUml2State().getRegions().iterator();
        			while (regionsIt.hasNext()) {
        				com.cea.papyrus.umlutils.Region region = new com.cea.papyrus.umlutils.Region((org.eclipse.uml2.uml.Region) regionsIt.next());
        				
        				// Recursive call
        				states.addAll(region.getSimpleStates());
        			}
        		}
        		
        	}
        }
        
        return states;
    }
}
